package com.itheima.project.utils;

import com.alibaba.fastjson.JSONObject;
import com.itheima.project.basic.IBasicEnum;
import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.basic.BasicEnum;

import java.util.Date;

/**
 * @Description 构造ResponseWrap工具
 */
public class ResponseWrapBuild {

    public static <T> ResponseWrap<T> build(IBasicEnum basicEnumIntface, T t){

        //从SubjectContent中拿到主体对象
        String subject = SubjectContent.getSubject();
        JSONObject jsonObject = null;
        //当前已登录
        if (!EmptyUtil.isNullOrEmpty(subject)){
            jsonObject = JSONObject.parseObject(subject);
        }else {
            jsonObject = new JSONObject();
        }
        //构建对象
        return ResponseWrap.<T>builder()
                .code(basicEnumIntface.getCode())
                .msg(basicEnumIntface.getMsg())
                .operationTime(new Date())
                .subjectId(jsonObject.getLong("id"))
                .subjectName(jsonObject.getString("username"))
                .datas(t)
                .build();
    }

    /**
     * 公共成功响应方法
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResponseWrap<T> successBuild(T t){
        return ResponseWrapBuild.build(BasicEnum.SUCCEED,t);
    }

    /**
     * 公共失败响应方法
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResponseWrap<T> failedBuild(T t){
        return ResponseWrapBuild.build(BasicEnum.SYSYTEM_FAIL,t);
    }

}
